<template>
  <div id="picture_detail">
    <a-row :gutter="[16, 16]">
      <!--      图片预览-->
      <a-col :sm="24" :md="16" :xl="18">
        <a-card>
          <a-image :src="picture?.url" style="max-height: 600px; object-fit: contain" />
        </a-card>
      </a-col>
      <!--      图片信息区域-->
      <a-col :sm="24" :md="8" :xl="6">
        <a-card title="图片信息">
          <a-descriptions :column="1">
            <a-descriptions-item label="作者">
              <a-space>
                <a-avatar :size="24" :src="picture?.user?.userAvatar" />
                <div>{{ picture?.user?.userName }}</div>
              </a-space>
            </a-descriptions-item>
            <a-descriptions-item label="名称">
              {{ picture?.name ?? '未命名' }}
            </a-descriptions-item>
            <a-descriptions-item label="简介">
              {{ picture?.introduction ?? '-' }}
            </a-descriptions-item>
            <a-descriptions-item label="分类">
              {{ picture?.category ?? '默认' }}
            </a-descriptions-item>
            <a-descriptions-item label="标签">
              <a-tag v-for="tag in picture?.tags" :key="tag" color="blue">
                {{ tag }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="格式">
              {{ picture?.picFormat ?? '-' }}
            </a-descriptions-item>
            <a-descriptions-item label="宽度">
              {{ picture?.picWidth ?? '-' }}
            </a-descriptions-item>
            <a-descriptions-item label="高度">
              {{ picture?.picHeight ?? '-' }}
            </a-descriptions-item>
            <a-descriptions-item label="宽高比">
              {{ picture?.picScale ?? '-' }}
            </a-descriptions-item>
            <a-descriptions-item label="大小">
              {{ formatSize(picture?.picSize) }}
            </a-descriptions-item>
          </a-descriptions>
          <!--          图片操作-->
          <a-space wrap>
            <a-button :icon="h(DownloadOutlined)" type="primary" @click="doDownload"
              >免费下载
            </a-button>
            <a-button v-if="canEdit" :icon="h(EditOutlined)" type="primary" @click="doEdit"
              >编辑
            </a-button>
            <a-button v-if="canEdit" :icon="h(DeleteOutlined)" danger @click="doDelete"
              >删除
            </a-button>
          </a-space>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>
<script setup lang="ts">
import { computed, h, onMounted, ref } from 'vue'
import { deletePictureUsingPost, getPictureVoByIdUsingGet } from '@/api/tupianguanli.ts'
import { message } from 'ant-design-vue'
import { downloadImage, formatSize } from '@/utils'
import { DeleteOutlined, DownloadOutlined, EditOutlined } from '@ant-design/icons-vue'
import { userLoginUserInfo } from '@/stores/userLoginUserInfo.ts'
import { useRouter } from 'vue-router'

interface Props {
  id: string | number
}

const props = defineProps<Props>()
const picture = ref<API.PictureVO>()
const loginUserStore = userLoginUserInfo()
const router = useRouter()

const doDownload = () => {
  downloadImage(picture.value?.url)
}
/**
 * 编辑图片
 */
const doEdit = async () => {
  await router.push({
    path: '/add_picture',
    query: {
      id: picture.value?.id,
    },
  })
}
/**
 * 删除图片
 */
const doDelete = async () => {
  try {
    const res = await deletePictureUsingPost({
      id: picture.value?.id,
    })
    if (res.data.code === 0) {
      message.success('删除成功')
      await router.replace('/')
    } else {
      message.error('删除失败，' + res.data.message)
    }
  } catch (e) {
    message.error('删除失败' + e)
  }
}

/**
 * 检查当前用户是否可以编辑图片
 */
const canEdit = computed(() => {
  const loginUser = loginUserStore.loginUser
  //未登录不可以编辑
  if (!loginUser.id) {
    return false
  }
  // 当前用户是作者或者管理员可以编辑
  const user = picture.value?.user || {}
  return loginUser.id === user?.id || loginUser.userRole === 'admin'
})
/**
 * 获取图片信息
 */
const fetchPictureDetail = async () => {
  try {
    const res = await getPictureVoByIdUsingGet({
      id: props.id as any,
    })
    if (res.data.code === 0 && res.data.data) {
      picture.value = res.data.data
      message.success('获取图片成功')
    } else {
      message.error('获取图片失败' + res.data?.message)
    }
  } catch (e) {
    message.error('获取图片失败' + e)
  }
}
//  初始化
onMounted(() => {
  fetchPictureDetail()
})
</script>
<style>
#picture_detail {
}
</style>
